package com.zzh.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzh.controller.utils.R;
import com.zzh.mapper.*;
import com.zzh.ov.UserRegister;
import com.zzh.pojo.*;
import com.zzh.service.UserService;
import com.zzh.service.VideoService;
import com.zzh.ut.MyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;

/**
 *
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CaptchaMapper captchaMapper;
    @Autowired
    private UserLoginInfoMapper userLoginInfoMapper;


    /**
     * 注册用户
     *
     * @param user
     * @return
     */
    @Override
    public R addUser(UserRegister user) {
        if (!MyUtils.isEmail(user.getEmail())) {
            return R.error("请输入正确的邮箱!");
        }
        if (user.getUname() == null) {
            user.setUname(user.getEmail());
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>();
        wrapper.eq(User::getEmail, user.getEmail());
        User dbUser = userMapper.selectOne(wrapper);
        //判断用户是否发送验证码
        LambdaQueryWrapper<Captcha> captchaLambdaQueryWrapper = new LambdaQueryWrapper<>();
        captchaLambdaQueryWrapper.eq(Captcha::getEmail, user.getEmail());
        Captcha captcha = captchaMapper.selectOne(captchaLambdaQueryWrapper);
        if (dbUser != null) {
            //用户存在不可注册
            return R.error("用户已存在!");
        }
        //用户不存在可以注册
        if (captcha == null) {
            log.info(user.getEmail() + ",没有获取验证码!注册失败!");
            return R.error("请先获取验证码,注册失败!");
        }
        //判断用户输入的验证码是否与表中相同
        if (user.getCaptcha().equals(captcha.getCaptcha())) {
            Snowflake snowflake = IdUtil.getSnowflake(1, 1);
            long salt = snowflake.nextId();
            user.setSalt(String.valueOf(salt));
            user.setUpwd(SecureUtil.md5(user.getUpwd() + salt));
            user.setUname("邮箱用户-" + user.getEmail());
            int insert = userMapper.insert(user);
            log.info("用户:" + user.getEmail() + ",注册成功!");
            HashMap<String, Object> map = new HashMap<>();
            String info = "用户:" + user.getEmail() + ",注册成功!";
            return R.info(info);
        }
        return R.error("请校验验证码!");

    }

    /**
     * 用户登录
     */
    @Override
    public R Login(User user, HttpSession session) {
        if (user.getEmail() == null || user.getUpwd() == null) {
            return R.error("请检查必填参数!");
        }
        log.info("获取库里用户" + user.getUpwd());
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getEmail, user.getEmail());
        User dbUser = userMapper.selectOne(userLambdaQueryWrapper);
        log.info("获取库里盐");
        log.info(user.getUpwd() + ",盐:" + dbUser.getSalt());
        String md5 = MyUtils.getMD5(user.getUpwd() + dbUser.getSalt());
        userLambdaQueryWrapper.eq(User::getUpwd, md5);
        User userLogin = userMapper.selectOne(userLambdaQueryWrapper);
        if (userLogin == null) {
            return R.error("账号或密码错误!");
        }
        log.info(user.getEmail() + ":登录成功!");
        session.setAttribute("user-session", user);
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        log.info("生成token!");
        String token = MyUtils.getMD5(dbUser.getSalt() + timestamp.getTime());
        log.info(token);
        session.setAttribute("token", token);
        UserLoginInfo loginInfo = new UserLoginInfo();
        loginInfo.setEmail(user.getEmail());
        loginInfo.setLoginTime(new Date());
        userLoginInfoMapper.insert(loginInfo);
        HashMap<String, String> map = new HashMap<>();
        map.put("info", "登录成功!");
        map.put("token",token);
        return R.success(map);
    }
}




